package main

import (
	"context"
	"fmt"
	"time"
)

func main() {
	ctx, cancel := context.WithTimeout(context.TODO(), 3*time.Second)
	withCancel, _ := context.WithCancel(ctx)
	go Watch(ctx, "监控1")
	go Watch(withCancel, "监控2")
	fmt.Println("现在开始等待8秒,time=", time.Now().Unix())
	//cancelFunc()
	time.Sleep(time.Second * 3)
	time.Sleep(8 * time.Second)
	fmt.Println("等待8秒结束,准备调用cancel()函数，发现两个子协程已经结束了，time=", time.Now().Unix())

	cancel()

}
func Watch(ctx2 context.Context, name string) {
	// select case 当管道有值的时候才会退出
	for {
		select {
		case <-ctx2.Done():
			fmt.Println(name, "收到信号，监控退出,time=", time.Now().Unix())
			return
		default:
			fmt.Println(name, "goroutine监控中,time=", time.Now().Unix())
			time.Sleep(1 * time.Second)
		}

	}
}
